/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.duty.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.duty.entity.ZlmtMaintenanceRecord;
import com.thinkgem.jeesite.modules.duty.service.ZlmtMaintenanceRecordService;
import com.thinkgem.jeesite.modules.inventory.entity.HostInventory;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtHostsAccessory;
import com.thinkgem.jeesite.modules.inventory.service.HostInventoryService;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtHostsAccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 维护记录管理Controller
 * @author johnKee
 * @version 2016-07-17
 */
@Controller
@RequestMapping(value = "${adminPath}/duty/zlmtMaintenanceRecord")
public class ZlmtMaintenanceRecordController extends BaseController {

	@Autowired
	private ZlmtMaintenanceRecordService zlmtMaintenanceRecordService;
	@Autowired
	private HostInventoryService hostInventoryService;
	@Autowired
	private ZlmtHostsAccessoryService zlmtHostsAccessoryService;
	
	@ModelAttribute
	public ZlmtMaintenanceRecord get(@RequestParam(required=false) String id) {
		ZlmtMaintenanceRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zlmtMaintenanceRecordService.get(id);
		}
		if (entity == null){
			entity = new ZlmtMaintenanceRecord();
		}
		return entity;
	}
	

	@RequestMapping(value = {"list", ""})
	public String list(ZlmtMaintenanceRecord zlmtMaintenanceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZlmtMaintenanceRecord> page = zlmtMaintenanceRecordService.findPage(new Page<ZlmtMaintenanceRecord>(request, response), zlmtMaintenanceRecord);
		model.addAttribute("page", page);
		return "modules/duty/zlmtMaintenanceRecordList";
	}


	@RequestMapping(value = "form")
	public String form(ZlmtMaintenanceRecord zlmtMaintenanceRecord, Model model) {
		String hostId = zlmtMaintenanceRecord.getHostid();
		String accessoryId = zlmtMaintenanceRecord.getAccessoryId();
		if (StringUtils.isNotEmpty(hostId)) {
			//根据主机id获取主机信息
			HostInventory hostInventory = hostInventoryService.get(zlmtMaintenanceRecord.getHostid());
			if (hostInventory != null) {
				zlmtMaintenanceRecord.setHostid(hostId);
				zlmtMaintenanceRecord.setHostname(hostInventory.getName());
				zlmtMaintenanceRecord.setAlias(hostInventory.getAlias());
			}
		}
		if (StringUtils.isNotEmpty(accessoryId)) {
			zlmtMaintenanceRecord.setAccessoryId(accessoryId);
			//根据配件id获取配件信息
			ZlmtHostsAccessory zlmtHostsAccessory = zlmtHostsAccessoryService.get(accessoryId);
			zlmtMaintenanceRecord.setAccessoryName(zlmtHostsAccessory.getAccessoryName());
				}
		model.addAttribute("zlmtMaintenanceRecord", zlmtMaintenanceRecord);
		return "modules/duty/zlmtMaintenanceRecordForm";
	}


	@RequestMapping(value = "save")
	public String save(ZlmtMaintenanceRecord zlmtMaintenanceRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zlmtMaintenanceRecord)){
			return form(zlmtMaintenanceRecord, model);
		}
		String hostId = zlmtMaintenanceRecord.getHostid();
		String accessoryId = zlmtMaintenanceRecord.getAccessoryId();
		if (StringUtils.isNotEmpty(hostId)) {
			//根据主机id获取主机信息
			HostInventory hostInventory = hostInventoryService.get(zlmtMaintenanceRecord.getHostid());
			if (hostInventory != null) {
				zlmtMaintenanceRecord.setHostid(hostId);
				zlmtMaintenanceRecord.setHostname(hostInventory.getName());
				zlmtMaintenanceRecord.setAlias(hostInventory.getAlias());

			}else{
				model.addAttribute("查询主机信息失败");
				return form(zlmtMaintenanceRecord, model);
			}
		} else {
			model.addAttribute("主机信息不能为空");
			return form(zlmtMaintenanceRecord, model);
		}
		if (StringUtils.isNotEmpty(accessoryId)) {
			zlmtMaintenanceRecord.setAccessoryId(accessoryId);
			//根据配件id获取配件信息
			ZlmtHostsAccessory zlmtHostsAccessory = zlmtHostsAccessoryService.get(accessoryId);
			if (zlmtHostsAccessory != null) {

			}else{
				model.addAttribute("查询配件信息失败");
				return form(zlmtMaintenanceRecord, model);
			}
		}else {
			model.addAttribute("配件信息不能为空");
			return form(zlmtMaintenanceRecord, model);
		}
		zlmtMaintenanceRecordService.save(zlmtMaintenanceRecord);
		addMessage(redirectAttributes, "保存维护记录成功");
		return "redirect:" + Global.getAdminPath() + "/duty/zlmtMaintenanceRecord/?repage";
	}
	

	@RequestMapping(value = "delete")
	public String delete(ZlmtMaintenanceRecord zlmtMaintenanceRecord, RedirectAttributes redirectAttributes) {
		zlmtMaintenanceRecordService.delete(zlmtMaintenanceRecord);
		addMessage(redirectAttributes, "删除维护记录成功");
		return "redirect:"+Global.getAdminPath()+"/duty/zlmtMaintenanceRecord/?repage";
	}

}